package com.ruoyi.riskrule.config;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.CustomerXdsReport;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.Func;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.riskrule.config.common.RuleUtils;
import com.ruoyi.riskrule.domain.RuleInNextStepXds;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.unit.DataUnit;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.*;

/**
 * @author mac
 */
@Slf4j
public class XdsWriteFactory {


    public static final String defaultTime = "2000-01-01 00:00:00";

    public Object setXds(RuleInNextStepXds ruleInNextStep, CustomerXdsReport ctcustomerXdsReport) {
        JSONObject creditAccountSummary = JSONObject.parseObject(ctcustomerXdsReport.getCreditAccountSummary());
        if (creditAccountSummary != null) {
            ruleInNextStep.setTotalaccounts1(creditAccountSummary.getString("TotalAccounts1"));
            if (StringUtils.isNotBlank(creditAccountSummary.getString("TotalMonthlyInstalment"))) {
                ruleInNextStep.setMonthlyinstalment(creditAccountSummary.getString("TotalMonthlyInstalment").replace(",", ""));
            }
            ruleInNextStep.setTotalaccounts(creditAccountSummary.getString("TotalAccounts"));//总账户数
            ruleInNextStep.setAccountingodcondition(creditAccountSummary.getString("TotalaccountinGodcondition"));//状况良好总账户数
            ruleInNextStep.setTotalnumberofdishonoured(creditAccountSummary.getString("TotalNumberofDishonoured"));//拒付总数
            ruleInNextStep.setAmountarrear(creditAccountSummary.getString("Amountarrear").replace(",", ""));//欠款金额
            ruleInNextStep.setTotaljudgementamount(creditAccountSummary.getString("TotalJudgementAmount"));//判决总额
            ruleInNextStep.setNumberofjudgement(creditAccountSummary.getString("TotalNumberofJudgement"));//判断书总数
            if (StringUtils.isNotBlank(creditAccountSummary.getString("TotalOutstandingdebt"))) {
                ruleInNextStep.setOutstandingdebt(creditAccountSummary.getString("TotalOutstandingdebt").replace(",", ""));//未偿债务总额
            }
            if (StringUtils.isNotBlank(creditAccountSummary.getString("TotalDishonouredAmount"))) {
                ruleInNextStep.setTotaldishonouredamount(creditAccountSummary.getString("TotalDishonouredAmount").replace(",", ""));//拒付总额
            }
            ruleInNextStep.setLastjudgementdate(creditAccountSummary.getString("LastJudgementDate"));//最后审判日
            ruleInNextStep.setTotalaccountarrear(creditAccountSummary.getString("TotalAccountarrear"));//总欠款账户数
            ruleInNextStep.setXdsrating(creditAccountSummary.getString("Rating"));//征信还款评分
        }

        JSONObject deliquencyInformation = JSONObject.parseObject(ctcustomerXdsReport.getDeliquencyinformation());
        if (deliquencyInformation != null) {
            ruleInNextStep.setPeriodnum(deliquencyInformation.getString("PeriodNum"));//周期数
            ruleInNextStep.setSubscribername(deliquencyInformation.getString("SubscriberName"));//⽤户名称
            ruleInNextStep.setMonthsinarrears(deliquencyInformation.getString("MonthsinArrears"));//欠款月数
            ruleInNextStep.setAccountno(deliquencyInformation.getString("AccountNo"));//账户号码
        }

        JSONObject accountRating = JSONObject.parseObject(ctcustomerXdsReport.getAccountRating());
        if (accountRating != null) {
            ruleInNextStep.setNoofpersonalloanaccountsgood(accountRating.getString("NoOfPersonalLoanAccountsGood"));//个人贷款良好数
            ruleInNextStep.setNoofpersonalloanaccountsbad(accountRating.getString("NoOfPersonalLoanAccountsBad"));//个人贷款不良数
            ruleInNextStep.setNoofhomeloanaccountsgood(accountRating.getString("NoOfHomeLoanAccountsGood"));//家庭贷款良好数
            ruleInNextStep.setNoofhomeloanaccountsbad(accountRating.getString("NoOfHomeLoanAccountsBad"));//家庭贷款不良数
            ruleInNextStep.setNoofautoloanccountsgood(accountRating.getString("NoOfAutoLoanccountsGood"));//汽车贷款良好数
            ruleInNextStep.setNoofautoloanaccountsbad(accountRating.getString("NoOfAutoLoanAccountsBad"));//汽车贷款不良数
            ruleInNextStep.setNoofstudyloanaccountsgood(accountRating.getString("NoOfStudyLoanAccountsGood"));//助学贷款良好数
            ruleInNextStep.setNoofstudyloanaccountsbad(accountRating.getString("NoOfStudyLoanAccountsBad"));//助学贷款不良数
            ruleInNextStep.setNoofcreditcardaccountsgood(accountRating.getString("NoOfCreditCardAccountsGood"));//信⽤卡良好数
            ruleInNextStep.setNoofcreditcardaccountsbad(accountRating.getString("NoOfCreditCardAccountsBad"));//信⽤卡不良数
            ruleInNextStep.setNooftelecomaccountsgood(accountRating.getString("NoOfTelecomAccountsGood"));//电信账户良好数
            ruleInNextStep.setNooftelecomaccountsbad(accountRating.getString("NoOfTelecomAccountsBad"));//电信账户不良数
            ruleInNextStep.setNoofjointloanaccountsgood(accountRating.getString("NoOfJointLoanAccountsGood"));//联名贷款账户良好数
            ruleInNextStep.setNoofjointloanaccountsbad(accountRating.getString("NoOfJointLoanAccountsBad"));//联名贷款账户不良数
            ruleInNextStep.setNoofretailaccountsbad(accountRating.getString("NoOfRetailAccountsBad"));//零售不良数
            ruleInNextStep.setNoofretailaccountsgood(accountRating.getString("NoOfRetailAccountsGood"));//零售良好数
            ruleInNextStep.setNoofotheraccountsgood(accountRating.getString("NoOfOtherAccountsGood"));//其他账户良好数
            ruleInNextStep.setNoofotheraccountsbad(accountRating.getString("NoOfOtherAccountsBad"));//其他账户不良数
        }

        String accountMonthlyPaymentHistoryStr = ctcustomerXdsReport.getAccountMonthlyPaymentHistory();
        JSONArray accountMonthlyPaymentHistory = new JSONArray();
        if (StringUtils.isNotBlank(accountMonthlyPaymentHistoryStr)) {
            if (accountMonthlyPaymentHistoryStr.startsWith("{")) {
                accountMonthlyPaymentHistory.add(JSONObject.parseObject(ctcustomerXdsReport.getAccountMonthlyPaymentHistory()));
            } else {
                accountMonthlyPaymentHistory = JSONArray.parseArray(ctcustomerXdsReport.getAccountMonthlyPaymentHistory());
            }
        }

        analyseXds(ruleInNextStep, ctcustomerXdsReport);

        List<Integer> xdsrating1mListAll = new ArrayList<>();
        List<Integer> xdsrating3mListAll = new ArrayList<>();
        List<Integer> xdsrating6mListAll = new ArrayList<>();
        List<Integer> xdsrating12mListAll = new ArrayList<>();

        Set<String> performingSet = new HashSet<>();
        Set<String> lostSet = new HashSet<>();
        Set<String> doubtfulSet = new HashSet<>();
        Set<String> substandardSet = new HashSet<>();
        Set<String> overdraftCntSet = new HashSet<>();
        Set<String> overdraftOdCntSet = new HashSet<>();
        BigDecimal overdraftOdAmt = BigDecimal.ZERO;
        List<Integer> overdraftOdDaysList = new ArrayList<>();
        List<Integer> overdraftoddaysvalidList = new ArrayList<>();
        Set<String> nooverdraftCntSet = new HashSet<>();
        Set<String> nooverdraftOdCntSet = new HashSet<>();
        BigDecimal nooverdraftOdAmt = BigDecimal.ZERO;
        List<Integer> nooverdraftOdDaysList = new ArrayList<>();
        List<Integer> nooverdraftoddaysvalidList = new ArrayList<>();
        List<Date> lastRepayDateList = new ArrayList<>();

        for (int i = 0; i < accountMonthlyPaymentHistory.size(); i++) {
            List<Integer> xdsrating1mList = new ArrayList<>();
            List<Integer> xdsrating3mList = new ArrayList<>();
            List<Integer> xdsrating6mList = new ArrayList<>();
            List<Integer> xdsrating12mList = new ArrayList<>();
            List<Integer> xdsrating24mList = new ArrayList<>();

            JSONObject jsonObject1 = accountMonthlyPaymentHistory.getJSONObject(i);

            String lastPaymentDate = jsonObject1.getString("LastPaymentDate");

            if (StringUtils.isNotBlank(lastPaymentDate)) {
                try {
                    Date date = DateUtils.parseDate(lastPaymentDate, "dd/MM/yyyy");
                    lastRepayDateList.add(date);
                } catch (Exception e) {
                    log.error("ClosedDate异常", e);
                }
            }

            xdsrating1mList.add(getMData(jsonObject1, "M01"));
            xdsrating3mList.add(getMData(jsonObject1, "M02"));
            xdsrating3mList.add(getMData(jsonObject1, "M03"));
            xdsrating6mList.add(getMData(jsonObject1, "M04"));
            xdsrating6mList.add(getMData(jsonObject1, "M05"));
            xdsrating6mList.add(getMData(jsonObject1, "M06"));
            xdsrating12mList.add(getMData(jsonObject1, "M07"));
            xdsrating12mList.add(getMData(jsonObject1, "M08"));
            xdsrating12mList.add(getMData(jsonObject1, "M09"));
            xdsrating12mList.add(getMData(jsonObject1, "M10"));
            xdsrating12mList.add(getMData(jsonObject1, "M11"));
            xdsrating12mList.add(getMData(jsonObject1, "M12"));
            xdsrating1mListAll.addAll(xdsrating1mList);
            xdsrating3mListAll.addAll(xdsrating3mList);
            xdsrating6mListAll.addAll(xdsrating6mList);
            xdsrating12mListAll.addAll(xdsrating12mList);
            xdsrating24mList.addAll(xdsrating1mList);
            xdsrating24mList.addAll(xdsrating3mList);
            xdsrating24mList.addAll(xdsrating6mList);
            xdsrating24mList.addAll(xdsrating12mList);
            xdsrating24mList.add(getMData(jsonObject1, "M13"));
            xdsrating24mList.add(getMData(jsonObject1, "M14"));
            xdsrating24mList.add(getMData(jsonObject1, "M15"));
            xdsrating24mList.add(getMData(jsonObject1, "M16"));
            xdsrating24mList.add(getMData(jsonObject1, "M17"));
            xdsrating24mList.add(getMData(jsonObject1, "M18"));
            xdsrating24mList.add(getMData(jsonObject1, "M19"));
            xdsrating24mList.add(getMData(jsonObject1, "M20"));
            xdsrating24mList.add(getMData(jsonObject1, "M21"));
            xdsrating24mList.add(getMData(jsonObject1, "M22"));
            xdsrating24mList.add(getMData(jsonObject1, "M23"));
            xdsrating24mList.add(getMData(jsonObject1, "M24"));

            String accountNo = jsonObject1.getString("AccountNo");
            String performanceStatus = jsonObject1.getString("PerformanceStatus");
            if ("Performing".equals(performanceStatus)) {
                performingSet.add(accountNo);
            } else if ("Lost".equals(performanceStatus)) {
                lostSet.add(accountNo);
            } else if ("Doubtful".equals(performanceStatus)) {
                doubtfulSet.add(accountNo);
            } else if ("Substandard".equals(performanceStatus)) {
                substandardSet.add(accountNo);
            }

            String indicatorDescription = jsonObject1.getString("IndicatorDescription");
            if (indicatorDescription != null) {
                indicatorDescription = indicatorDescription.toLowerCase(Locale.ROOT);
                if (indicatorDescription.contains("overdraft")) {
                    overdraftCntSet.add(accountNo);
                    overdraftOdDaysList.add(getMData(jsonObject1, "M01"));
                    if (getMData(jsonObject1, "M01") > 0) {
                        overdraftOdCntSet.add(accountNo);
                        overdraftOdAmt = overdraftOdAmt.add(getBigDecimal(jsonObject1.getString("AmountOverdue")));
                    }
                    for (int j = 0; j < xdsrating24mList.size(); j++) {
                        if (xdsrating24mList.get(j) > 0) {
                            overdraftoddaysvalidList.add(xdsrating24mList.get(j));
                            break;
                        }
                    }
                } else {
                    nooverdraftCntSet.add(accountNo);
                    nooverdraftOdDaysList.add(getMData(jsonObject1, "M01"));
                    if (getMData(jsonObject1, "M01") > 0) {
                        nooverdraftOdCntSet.add(accountNo);
                        nooverdraftOdAmt = nooverdraftOdAmt.add(getBigDecimal(jsonObject1.getString("AmountOverdue")));
                    }
                    for (int j = 0; j < xdsrating24mList.size(); j++) {
                        if (xdsrating24mList.get(j) > 0) {
                            nooverdraftoddaysvalidList.add(xdsrating24mList.get(j));
                            break;
                        }
                    }
                }
            }
        }

        xdsrating3mListAll.addAll(xdsrating1mListAll);
        xdsrating6mListAll.addAll(xdsrating3mListAll);
        xdsrating12mListAll.addAll(xdsrating6mListAll);

        ruleInNextStep.setXdsrating1m(xdsrating1mListAll.size() == 0 ? "0" : Collections.max(xdsrating1mListAll) + "");//近一个月还款评分最高分
        ruleInNextStep.setXdsrating3m(xdsrating3mListAll.size() == 0 ? "0" : Collections.max(xdsrating3mListAll) + "");//近三个月还款评分最高分
        ruleInNextStep.setXdsrating6m(xdsrating6mListAll.size() == 0 ? "0" : Collections.max(xdsrating6mListAll) + "");//近6个月还款评分最高分
        ruleInNextStep.setXdsrating12m(xdsrating12mListAll.size() == 0 ? "0" : Collections.max(xdsrating12mListAll) + "");//近12个月还款评分最高分

        ruleInNextStep.setNumsubstandard(substandardSet.size() + "");//次贷合同数
        ruleInNextStep.setNumlost(lostSet.size() + "");//坏账合同数
        ruleInNextStep.setNumperforming(performingSet.size() + "");//在贷合同数
        ruleInNextStep.setNumdoubtful(doubtfulSet.size() + "");//呆账合同数

        ruleInNextStep.setOverdraftcnt(overdraftCntSet.size() + "");//XDS征信Overdraft订单数
        ruleInNextStep.setOverdraftodcnt(overdraftOdCntSet.size() + "");//XDS征信Overdraft逾期订单数
        ruleInNextStep.setOverdraftodamt(overdraftOdAmt.toString());//XDS征信Overdraft逾期金额
        ruleInNextStep.setOverdraftoddays(overdraftOdDaysList.size() == 0 ? "0" : Collections.max(overdraftOdDaysList) + "");//XDS征信OverdraftM01最大逾期天数
        ruleInNextStep.setOverdraftoddaysvalid(overdraftoddaysvalidList.size() == 0 ? "0" : Collections.max(overdraftoddaysvalidList) + "");//XDS征信Overdraft最近有效逾期天数
        ruleInNextStep.setNooverdraftcnt(nooverdraftCntSet.size() + "");//XDS征信非Overdraft订单数
        ruleInNextStep.setNooverdraftodcnt(nooverdraftOdCntSet.size() + "");//XDS征信非Overdraft逾期订单数
        ruleInNextStep.setNooverdraftodamt(nooverdraftOdAmt.toString());//XDS征信非Overdraft逾期金额
        ruleInNextStep.setNooverdraftoddays(nooverdraftOdDaysList.size() == 0 ? "0" : Collections.max(nooverdraftOdDaysList) + "");//XDS征信非OverdraftM01最大逾期天数
        ruleInNextStep.setNooverdraftoddaysvalid(nooverdraftoddaysvalidList.size() == 0 ? "0" : Collections.max(nooverdraftoddaysvalidList) + "");//XDS征信非Overdraft最近有效逾期天数

        if (lastRepayDateList.size() != 0) {
            double dateDiff = DateUtils.getDateDiff(DateUtils.DATE_INTERVAL_DAY, new Date(), lastRepayDateList.size() == 0 ? new Date() : Collections.max(lastRepayDateList));
            ruleInNextStep.setLastrepaydate((int) dateDiff + "");//最后一次还款时间距当前申请时间
        }

        String creditAgreementSummaryStr = ctcustomerXdsReport.getCreditAgreementSummary();
        JSONArray creditAgreementSummary = new JSONArray();
        if (StringUtils.isNotBlank(creditAgreementSummaryStr)) {
            if (creditAgreementSummaryStr.startsWith("{")) {
                creditAgreementSummary.add(JSONObject.parseObject(ctcustomerXdsReport.getCreditAgreementSummary()));
            } else {
                creditAgreementSummary = JSONArray.parseArray(ctcustomerXdsReport.getCreditAgreementSummary());
            }
        }

        List<Integer> xdsclosedopendateList = new ArrayList<>();
        List<Date> closeDate = new ArrayList<>();
        List<Date> opendDate = new ArrayList<>();
        Set<Object> accountNoSet = new HashSet<>();

        int xdsnumperformingaccount = 0;
        double openingbalanceamt = 0;
        double xdsmaxinstalmentamount = 0;
        double xdsmaxcurrentbalanceamt = 0;

        int xdsOpenMacth90dNum = 0;
        int xdsOpenMacth60dNum = 0;
        int xdsOpenMacth30dNum = 0;


        for (int i = 0; i < creditAgreementSummary.size(); i++) {
            JSONObject jsonObject = creditAgreementSummary.getJSONObject(i);
            try {
                if (StringUtils.isNotBlank(jsonObject.getString("ClosedDate"))) {
                    closeDate.add(DateUtils.parse(jsonObject.getString("ClosedDate"), "dd/MM/yyyy"));
                }
                if (StringUtils.isNotBlank(jsonObject.getString("DateAccountOpened"))) {
                    Date ccountOpenedate = DateUtils.parse(jsonObject.getString("DateAccountOpened"), "dd/MM/yyyy");
                    opendDate.add(ccountOpenedate);
                    double openMacthDf = DateUtils.getDateDiff(DateUtils.DATE_INTERVAL_DAY, ctcustomerXdsReport.getCreateTime(), ccountOpenedate);
                    if (openMacthDf < 90) {
                        xdsOpenMacth90dNum++;
                    }
                    if (openMacthDf < 60) {
                        xdsOpenMacth60dNum++;
                    }
                    if (openMacthDf < 30) {
                        xdsOpenMacth30dNum++;
                    }
                }
                if (StringUtils.isNotBlank(jsonObject.getString("ClosedDate")) && StringUtils.isNotBlank(jsonObject.getString("DateAccountOpened"))) {
                    double dateDiff = DateUtils.getDateDiff(DateUtils.DATE_INTERVAL_DAY, DateUtils.parse(jsonObject.getString("ClosedDate"), "dd/MM/yyyy"), DateUtils.parse(jsonObject.getString("DateAccountOpened"), "dd/MM/yyyy"));
                    xdsclosedopendateList.add(new BigDecimal(dateDiff).divide(new BigDecimal("30"), 0, BigDecimal.ROUND_UP).intValue());
                }
            } catch (Exception e) {
                log.error("ClosedDate异常", e);
            }

            //accountNo去重
            if (!accountNoSet.contains(jsonObject.get("AccountNo"))) {

                if ("Performing".equals(jsonObject.get("PerformanceStatus"))) {
                    xdsnumperformingaccount++;
                }
                accountNoSet.add(jsonObject.get("AccountNo"));
            }

            Double openingBalanceAmt = Double.valueOf(
                    (Func.isEmpty(jsonObject.get("OpeningBalanceAmt"))
                            ? "-1" : jsonObject.getString("OpeningBalanceAmt")).replace(",", "")
            );
            openingbalanceamt = openingBalanceAmt > openingbalanceamt ? openingBalanceAmt : openingbalanceamt;

            Double instalmentAmount = Double.valueOf(StringUtils.isBlank(jsonObject.getString("InstalmentAmount"))
                    ? "-1" : jsonObject.getString("InstalmentAmount").replace(",", ""));
            xdsmaxinstalmentamount = instalmentAmount > xdsmaxinstalmentamount ? instalmentAmount : xdsmaxinstalmentamount;

            Double currentBalanceAmt = Double.valueOf(StringUtils.isBlank(jsonObject.getString("CurrentBalanceAmt"))
                    ? "-1" : jsonObject.getString("CurrentBalanceAmt").replace(",", ""));
            xdsmaxcurrentbalanceamt = currentBalanceAmt > xdsmaxcurrentbalanceamt ? currentBalanceAmt : xdsmaxcurrentbalanceamt;

            ZoneId zoneId = ZoneId.systemDefault();
        }

        ruleInNextStep.setXdsnumperformingaccount(xdsnumperformingaccount + "");
        ruleInNextStep.setXdsmaxopeningbalanceamt(openingbalanceamt + "");
        ruleInNextStep.setXdsmaxinstalmentamount(xdsmaxinstalmentamount + "");
        ruleInNextStep.setXdsmaxcurrentbalanceamt(xdsmaxcurrentbalanceamt + "");

        ruleInNextStep.setXdsopenmacth90d(xdsOpenMacth90dNum + "");
        ruleInNextStep.setXdsopenmacth60d(xdsOpenMacth60dNum + "");
        ruleInNextStep.setXdsopenmacth30d(xdsOpenMacth30dNum + "");

        if (closeDate.size() > 0 && opendDate.size() > 0) {
            double dateDiff = DateUtils.getDateDiff(DateUtils.DATE_INTERVAL_DAY, closeDate.size() == 0 ? new Date() : Collections.max(closeDate), opendDate.size() == 0 ? new Date() : Collections.min(opendDate));
            BigDecimal bigDecimal = new BigDecimal(dateDiff).divide(new BigDecimal("30"), 0, BigDecimal.ROUND_UP);
            ruleInNextStep.setUpdatedOpenDate(bigDecimal + "");
        }

        ruleInNextStep.setXdsclosedopendate(xdsclosedopendateList.size() == 0 ? "0" : Collections.max(xdsclosedopendateList) + "");//历史最长借款期限(月）


        JSONObject guarantorCount = JSONObject.parseObject(ctcustomerXdsReport.getGuarantorCount());
        if (Func.isNotEmpty(guarantorCount)) {
            Object accounts = guarantorCount.get("Accounts");
            Object guarantorsSecured = guarantorCount.get("GuarantorsSecured");
            ruleInNextStep.setGuarantorcountaccounts(accounts == null ? "-1" : accounts.toString());
            ruleInNextStep.setGuarantorcountguarantorssecured(guarantorsSecured == null ? "-1" : guarantorsSecured.toString());
        }

        //enquiryhistorytopStr
        String enquiryhistorytopStr = ctcustomerXdsReport.getEnquiryHistoryTop();
        JSONArray enquiryhistorytop = new JSONArray();
        if (StringUtils.isNotBlank(enquiryhistorytopStr)) {
            if (enquiryhistorytopStr.startsWith("{")) {
                enquiryhistorytop.add(JSONObject.parseObject(enquiryhistorytopStr));
            } else {
                enquiryhistorytop = JSONArray.parseArray(enquiryhistorytopStr);
            }
        }
        log.info("enquiryhistorytopStr============="+enquiryhistorytopStr);
        int xdsenquiryreasonborrower = 0;
        int xdscntenquiry = 0;
        JSONObject newEnquiryhistorytop = null;
        String DateRequested = defaultTime;
        for (int i = 0;i<enquiryhistorytop.size();i++){
            JSONObject jsonObject = enquiryhistorytop.getJSONObject(i);

            try{
                if(newEnquiryhistorytop!=null){
                    Date date1 = DateUtils.parseDate(jsonObject.getString("DateRequested"), "dd/MM/yyyy HH:mm:sss");
                    Date date2 = DateUtils.parseDate(newEnquiryhistorytop.getString("DateRequested"), "dd/MM/yyyy HH:mm:sss");

                    if(date1.compareTo(date2)==1){
                        newEnquiryhistorytop = jsonObject;
                        DateRequested = DateUtils.formatDate(date1,"yyyy-MM-dd HH:mm:ss");
                    }
                }else{
                    Date date1 = DateUtils.parseDate(jsonObject.getString("DateRequested"), "dd/MM/yyyy HH:mm:sss");
                    DateRequested = DateUtils.formatDate(date1,"yyyy-MM-dd HH:mm:ss");
                    newEnquiryhistorytop = jsonObject;
                }
            }catch (Exception e){
                log.error("日期异常{},{}",jsonObject.getString("DateRequested"),newEnquiryhistorytop.getString("DateRequested"),e);
            }

            if(StringUtils.isNotBlank(jsonObject.getString("SubscriberEnquiryResultID"))){
                xdscntenquiry++;
            }

            if(StringUtils.isNotBlank(jsonObject.getString("EnquiryReason")) && jsonObject.getString("EnquiryReason").toLowerCase().contains("borrower")){
                xdsenquiryreasonborrower++;
            }

        }

        if(newEnquiryhistorytop!=null){
            ruleInNextStep.setEnquiryhistorytopsubscriberenquiryresultid(newEnquiryhistorytop.getString("SubscriberEnquiryResultID"));
            ruleInNextStep.setEnquiryhistorytopsubscribername(newEnquiryhistorytop.getString("SubscriberName"));
            ruleInNextStep.setEnquiryhistorytopenquiryreason(newEnquiryhistorytop.getString("EnquiryReason"));
        }

        ruleInNextStep.setEnquiryhistorytopdaterequested(DateRequested);
        ruleInNextStep.setXdscntenquiry(xdscntenquiry+"");
        ruleInNextStep.setXdsenquiryreasonborrower(xdsenquiryreasonborrower+"");

        return afterFormatRuleInNextStepXds(ruleInNextStep);
    }


    public void analyseXds(RuleInNextStepXds ruleInNextStep, CustomerXdsReport ctcustomerXdsReport) {
        analyseAccountMonthlyPaymentHistoryElementData(ruleInNextStep, ctcustomerXdsReport);
    }

    /**
     * 获取 AccountMonthlyPaymentHistory 节点的数据
     */
    public void analyseAccountMonthlyPaymentHistoryElementData(RuleInNextStepXds ruleInNextStep, CustomerXdsReport ctcustomerXdsReport) {


        String accountMonthlyPaymentHistoryString = ctcustomerXdsReport.getAccountMonthlyPaymentHistory();
        if (StringUtils.isBlank(accountMonthlyPaymentHistoryString)) {
            return;
        }

        JSONArray accountMonthlyPaymentHistory = new JSONArray();

        if (accountMonthlyPaymentHistoryString.startsWith("{")) {
            accountMonthlyPaymentHistory.add(JSONObject.parseObject(ctcustomerXdsReport.getAccountMonthlyPaymentHistory()));
        } else {
            accountMonthlyPaymentHistory = JSONArray.parseArray(accountMonthlyPaymentHistoryString);
        }

//        获取平均贷款金额
        Date createTime = ctcustomerXdsReport.getCreateTime();

        BigDecimal balance30 = BigDecimal.ZERO;
        BigDecimal balance90 = BigDecimal.ZERO;
        BigDecimal balance180 = BigDecimal.ZERO;
        BigDecimal instalment30 = BigDecimal.ZERO;
        BigDecimal instalment90 = BigDecimal.ZERO;
        BigDecimal instalment180 = BigDecimal.ZERO;

        //            保存编号用于去重
        List<String> over30AccountNo = new ArrayList<>();
        List<String> over90AccountNo = new ArrayList<>();
        List<String> zeroNumAccountNo = new ArrayList<>();
        List<String> installmentAccountNo = new ArrayList<>();
        List<String> repayAccountNo = new ArrayList<>();

        int over30Num = 0;
        int over90Num = 0;


//        设置默认值
        ruleInNextStep.setXdspaymenthistoryamountbankavgac("-1");
        ruleInNextStep.setXdspaymenthistoryamountnonbankavgac("-1");
        ruleInNextStep.setXdspaymenthistoryamountbankavgac3m("-1");
        ruleInNextStep.setXdspaymenthistoryamountnonbankavgac3m("-1");
        ruleInNextStep.setXdspaymenthistoryamountbankavgac6m("-1");
        ruleInNextStep.setXdspaymenthistoryamountnonbankavgac6m("-1");

        analysePerformanceWithSubstandard(accountMonthlyPaymentHistory, ruleInNextStep,ctcustomerXdsReport);

        analysePerformanceWithLost(accountMonthlyPaymentHistory, ruleInNextStep,ctcustomerXdsReport);

        analysePerformanceWithDoubtful(accountMonthlyPaymentHistory, ruleInNextStep,ctcustomerXdsReport);

        analysePerformanceWithPerforming(accountMonthlyPaymentHistory, ruleInNextStep,ctcustomerXdsReport);

        analyseSubscriberNameContainBank(accountMonthlyPaymentHistory, ruleInNextStep,ctcustomerXdsReport);

        analyseSubscriberNameNotBank(accountMonthlyPaymentHistory, ruleInNextStep,ctcustomerXdsReport);

        for (int i = 0; i < accountMonthlyPaymentHistory.size(); i++) {
            JSONObject jsonObject = accountMonthlyPaymentHistory.getJSONObject(i);
            long rePay30 = DateUtils.getDayInterval(DateUtils.timeFormat(jsonObject.getString("LastPaymentDate")), DateUtils.getTimeFromDate(ctcustomerXdsReport.getCreateTime()));
//            近一个月还款笔数
            if (!repayAccountNo.contains(jsonObject.getString("AccountNo")) && rePay30 <= 30 && rePay30 >= 0) {
                repayAccountNo.add(jsonObject.getString("AccountNo"));
            }
//分期付款类借款合同数
            if (StringUtils.isNotBlank(jsonObject.getString("IndicatorDescription")) && (jsonObject.getString("IndicatorDescription").toLowerCase().contains("installment")
                    || jsonObject.getString("IndicatorDescription").toLowerCase().contains("fixed"))) {
                if (!installmentAccountNo.contains(jsonObject.getString("AccountNo"))) {
                    installmentAccountNo.add(jsonObject.getString("AccountNo"));
                }
            }
//            当前余额为零合同数
            if (getBigDecimal(jsonObject.getString("CurrentBalanceAmt")).compareTo(BigDecimal.ZERO) <= 0) {
                if (!zeroNumAccountNo.contains(jsonObject.getString("AccountNo"))) {
                    zeroNumAccountNo.add(jsonObject.getString("AccountNo"));
                }
            }

            long balance30Day = DateUtils.getDayInterval(DateUtils.getTimeFromDate(ctcustomerXdsReport.getCreateTime()), DateUtils.timeFormat(jsonObject.getString("LastUpdatedDate")));
//            近30天在贷余额
            if (balance30Day >= 0 && balance30Day <= 30) {
                balance30 = balance30.add(getBigDecimal(jsonObject.getString("CurrentBalanceAmt")));
                //                近30天分期付款额
                instalment30 = instalment30.add(getBigDecimal(jsonObject.getString("MonthlyInstalmentAmt")));
            }

            //            近90天在贷余额
            if (balance30Day >= 0 && balance30Day <= 90) {
                balance90 = balance90.add(getBigDecimal(jsonObject.getString("CurrentBalanceAmt")));
                //                近90天分期付款额
                instalment90 = instalment90.add(getBigDecimal(jsonObject.getString("MonthlyInstalmentAmt")));
            }

            //            近180天在贷余额
            if (balance30Day <= 180 && balance30Day >=0) {
                balance180 = balance180.add(getBigDecimal(jsonObject.getString("CurrentBalanceAmt")));
                //                近180天分期付款额
                instalment180 = instalment180.add(getBigDecimal(jsonObject.getString("MonthlyInstalmentAmt")));
            }


            if (StringUtils.isNotBlank(jsonObject.getString("SubscriberName"))) {

                if (!jsonObject.getString("SubscriberName").toLowerCase().contains("bank")) {
//                    近六个月非银行机构逾期超30天贷款笔数
                    if (!over30AccountNo.contains(jsonObject.getString("AccountNo"))) {
                        over30AccountNo.add(jsonObject.getString("AccountNo"));
                        over30Num += getSuitNum(jsonObject, 6, 30);
                    }
                    //                    近三个月非银行机构逾期超90天贷款笔数
                    if (!over90AccountNo.contains(jsonObject.getString("AccountNo"))) {
                        over90AccountNo.add(jsonObject.getString("AccountNo"));
                        over90Num += getSuitNum(jsonObject, 3, 90);
                    }
                }

            }

        }


        ruleInNextStep.setXdscurrentbalanceamt30d(balance30.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdscurrentbalanceamt90d(balance90.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdscurrentbalanceamt180d(balance180.setScale(0, BigDecimal.ROUND_DOWN).toString());

        ruleInNextStep.setXdsmonthlyinstalment30d(instalment30.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdsmonthlyinstalment90d(instalment90.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdsmonthlyinstalment180d(instalment180.setScale(0, BigDecimal.ROUND_DOWN).toString());


        ruleInNextStep.setXdscurrentbalanceamtltzeronum(zeroNumAccountNo.size() + "");
        ruleInNextStep.setXdsindicatorinstallmentnum(installmentAccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumrepay30d(repayAccountNo.size() + "");

        ruleInNextStep.setXpcntnobankover30d6m(over30Num + "");
        ruleInNextStep.setXpcntnobankover90d3m(over90Num + "");

    }

    private void analyseSubscriberNameNotBank(JSONArray accountMonthlyPaymentHistory, RuleInNextStepXds ruleInNextStep,CustomerXdsReport ctcustomerXdsReport) {
        List<String> otherAccountNo = new ArrayList<>();
        List<String> threeMonthNoBankAccountNo = new ArrayList<>();
        List<String> sixMonthNoBankAccountNo = new ArrayList<>();
        List<String> oneNoBankAccountNo = new ArrayList<>();
        Set<String> noBankNum = new HashSet<>();

        // 总和
        BigDecimal noBankAll = BigDecimal.ZERO;
        BigDecimal oneNoBankAll = BigDecimal.ZERO;
        BigDecimal threeNoBankAll = BigDecimal.ZERO;
        BigDecimal sixNoBankAll = BigDecimal.ZERO;

        BigDecimal sixMonthNoBankAvg = BigDecimal.ZERO;
        //        求平均
        BigDecimal otherAvg = BigDecimal.ZERO;
        BigDecimal threeMonthNoBankAvg = BigDecimal.ZERO;

        double noBankAmt = -1;
        Double noBankMinAmt = null;
        JSONObject pre = null;

        for (int i = 0; i < accountMonthlyPaymentHistory.size(); i++) {
            JSONObject jsonObject = accountMonthlyPaymentHistory.getJSONObject(i);

            if (StringUtils.isNotBlank(jsonObject.getString("SubscriberName"))) {
                if (!jsonObject.getString("SubscriberName").toLowerCase().contains("bank") && !jsonObject.getString("SubscriberName").toLowerCase().contains("eric vista")) {
//其他借贷机构数
                    noBankNum.add(jsonObject.getString("SubscriberName"));

//其他借贷机构最大贷款金额
                    noBankAmt = Math.max(noBankAmt, getBigDecimal(jsonObject.getString("OpeningBalanceAmt")).doubleValue());

                    if (noBankMinAmt == null) {
                        noBankMinAmt = getBigDecimal(jsonObject.getString("OpeningBalanceAmt")).doubleValue();
                    } else {
                        noBankMinAmt = Math.min(noBankMinAmt, getBigDecimal(jsonObject.getString("OpeningBalanceAmt")).doubleValue());
                    }
//其他借贷机构最近一笔贷款金额
                    if (pre == null) {
                        pre = jsonObject;
                    } else {
                        pre = maxDate(pre, jsonObject);
                    }

                    if (!otherAccountNo.contains(jsonObject.getString("AccountNo"))) {
                        otherAccountNo.add(jsonObject.getString("AccountNo"));
                        otherAvg = otherAvg.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                    }

                    int dateAccountOpened = DateUtils.getMonthInterval(DateUtils.getTimeFromDate(ctcustomerXdsReport.getCreateTime()),
                            DateUtils.timeFormat(jsonObject.getString("DateAccountOpened")));

                    //                    编号去重,判断是否是近1个月的数据
                    if (!oneNoBankAccountNo.contains(jsonObject.getString("AccountNo")) &&
                            dateAccountOpened >= 0 && dateAccountOpened <= 1
                    ) {
                        oneNoBankAccountNo.add(jsonObject.getString("AccountNo"));
                    }

                    //                    编号去重,判断是否是近3个月的数据
                    if (!threeMonthNoBankAccountNo.contains(jsonObject.getString("AccountNo")) &&
                            dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                        threeMonthNoBankAccountNo.add(jsonObject.getString("AccountNo"));
                        threeMonthNoBankAvg = threeMonthNoBankAvg.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                    }

                    //                    编号去重,判断是否是近6个月的数据
                    if (!sixMonthNoBankAccountNo.contains(jsonObject.getString("AccountNo")) &&
                            dateAccountOpened <= 6 && dateAccountOpened >= 0) {
                        sixMonthNoBankAccountNo.add(jsonObject.getString("AccountNo"));
                        sixMonthNoBankAvg = sixMonthNoBankAvg.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));

                    }

                    //判断是否是近1个月的数据
                    if (dateAccountOpened >= 0 && dateAccountOpened <= 1) {
                        oneNoBankAll = oneNoBankAll.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                    }
                    //判断是否是近3个月的数据
                    if (dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                        threeNoBankAll = threeNoBankAll.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));

                    }

                    //判断是否是近6个月的数据
                    if (dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                        sixNoBankAll = sixNoBankAll.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                    }

                    noBankAll = noBankAll.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));

                }
            }
        }

        if (otherAccountNo.size() != 0) {
            ruleInNextStep.setXdspaymenthistoryamountnonbankavgac(otherAvg.divide(new BigDecimal(otherAccountNo.size()), 0, BigDecimal.ROUND_DOWN).toString());
        }

        if (threeMonthNoBankAccountNo.size() != 0) {
            ruleInNextStep.setXdspaymenthistoryamountnonbankavgac3m(threeMonthNoBankAvg
                    .divide(new BigDecimal(threeMonthNoBankAccountNo.size()), 0, BigDecimal.ROUND_DOWN).toString()
            );
        }

        if (sixMonthNoBankAccountNo.size() != 0) {
            ruleInNextStep.setXdspaymenthistoryamountnonbankavgac6m(sixMonthNoBankAvg
                    .divide(new BigDecimal(sixMonthNoBankAccountNo.size()), 0, BigDecimal.ROUND_DOWN).toString()
            );
        }

        ruleInNextStep.setXdspaymenthistorycntnonbankavg1m(oneNoBankAccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistoryamountnonbanklast(pre == null ? "-1" : pre.getString("OpeningBalanceAmt"));
        ruleInNextStep.setXdspaymenthistorynumnonbank(noBankNum.size() + "");
        ruleInNextStep.setXdspaymenthistoryamountnonbankmax(noBankAmt + "");
        ruleInNextStep.setXdspaymenthistoryamountnonbankmin(noBankMinAmt == null ? "-1" : noBankMinAmt + "");
        ruleInNextStep.setXdspaymenthistorycntnonbankavg3m(new BigDecimal(threeMonthNoBankAccountNo.size()).divide(new BigDecimal("3"), 0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistorycntnonbankavg6m(new BigDecimal(sixMonthNoBankAccountNo.size()).divide(new BigDecimal("6"), 0, BigDecimal.ROUND_DOWN).toString());

        ruleInNextStep.setXdspaymenthistoryamountnonbankall(noBankAll.toString());
        ruleInNextStep.setXdspaymenthistoryamountnonbank1m(oneNoBankAll.toString());
        ruleInNextStep.setXdspaymenthistoryamountnonbank3m(threeNoBankAll.toString());
        ruleInNextStep.setXdspaymenthistoryamountnonbank6m(sixNoBankAll.toString());
        ruleInNextStep.setXdspaymenthistoryamountnonbankavg3m(threeNoBankAll.divide(new BigDecimal("3"), 0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountnonbankavg6m(sixNoBankAll.divide(new BigDecimal("6"), 0, BigDecimal.ROUND_DOWN).toString());


    }

    private void analyseSubscriberNameContainBank(JSONArray accountMonthlyPaymentHistory, RuleInNextStepXds ruleInNextStep,CustomerXdsReport ctcustomerXdsReport) {

        //        求平均
        BigDecimal avg = BigDecimal.ZERO;
        BigDecimal threeMonthAvg = BigDecimal.ZERO;
        BigDecimal sixMonthAvg = BigDecimal.ZERO;
// 总和
        BigDecimal bankAll = BigDecimal.ZERO;
        BigDecimal oneBankAll = BigDecimal.ZERO;
        BigDecimal threeBankAll = BigDecimal.ZERO;
        BigDecimal sixBankAll = BigDecimal.ZERO;

        List<String> accountNo = new ArrayList<>();
        List<String> threeMonthAccountNo = new ArrayList<>();
        List<String> sixMonthAccountNo = new ArrayList<>();
        List<String> oneBankAccountNo = new ArrayList<>();
        List<String> over60AccountNo = new ArrayList<>();
        Set<String> bankNum = new HashSet<>();
// 匹配数量
        int over60Num = 0;

        double preBalanceAmt = -1;
        Double preMinAmt = null;
        JSONObject pre = null;

        for (int i = 0; i < accountMonthlyPaymentHistory.size(); i++) {
            JSONObject jsonObject = accountMonthlyPaymentHistory.getJSONObject(i);

            if (StringUtils.isNotBlank(jsonObject.getString("SubscriberName"))) {
                if (jsonObject.getString("SubscriberName").toLowerCase().contains("bank")) {
//                    银行机构最小贷款金额
                    if (preMinAmt == null) {
                        preMinAmt = getBigDecimal(jsonObject.getString("OpeningBalanceAmt")).doubleValue();
                    } else {
                        preMinAmt = Math.min(preMinAmt, getBigDecimal(jsonObject.getString("OpeningBalanceAmt")).doubleValue());
                    }

//                    银行机构最近一笔贷款金额
                    if (pre == null) {
                        pre = jsonObject;
                    } else {
                        pre = maxDate(pre, jsonObject);
                    }
//                    银行机构最大贷款金额
                    preBalanceAmt = Math.max(preBalanceAmt, getBigDecimal(jsonObject.getString("OpeningBalanceAmt")).doubleValue());
//                    银行机构数
                    bankNum.add(jsonObject.getString("SubscriberName"));

//                    近十二个月银行机构逾期超60天贷款笔数
                    if (!over60AccountNo.contains(jsonObject.getString("AccountNo"))) {
                        over60AccountNo.add(jsonObject.getString("AccountNo"));
                        over60Num += getSuitNum(jsonObject, 12, 60);
                    }

//                    编号去重
                    if (!accountNo.contains(jsonObject.getString("AccountNo"))) {
                        accountNo.add(jsonObject.getString("AccountNo"));
                        avg = avg.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));

                    }
                    int dateAccountOpened = DateUtils.getMonthInterval(DateUtils.getTimeFromDate(ctcustomerXdsReport.getCreateTime()), DateUtils.timeFormat(jsonObject.getString("DateAccountOpened")));

                    //                    编号去重,判断是否是近1个月的数据
                    if (!oneBankAccountNo.contains(jsonObject.getString("AccountNo")) &&
                            dateAccountOpened >= 0 && dateAccountOpened <= 1
                    ) {
                        oneBankAccountNo.add(jsonObject.getString("AccountNo"));
                    }

                    //                    编号去重,判断是否是近3个月的数据
                    if (!threeMonthAccountNo.contains(jsonObject.getString("AccountNo")) &&
                            dateAccountOpened >= 0 && dateAccountOpened <= 3
                    ) {
                        threeMonthAccountNo.add(jsonObject.getString("AccountNo"));
                        threeMonthAvg = threeMonthAvg.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));

                    }

                    //                    编号去重,判断是否是近6个月的数据
                    if (!sixMonthAccountNo.contains(jsonObject.getString("AccountNo")) &&
                            dateAccountOpened >= 0 && dateAccountOpened <= 6
                    ) {
                        sixMonthAccountNo.add(jsonObject.getString("AccountNo"));
                        sixMonthAvg = sixMonthAvg.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                    }

                    bankAll = bankAll.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));

                    //判断是否是近1个月的数据
                    if (dateAccountOpened >= 0 && dateAccountOpened <= 1) {
                        oneBankAll = oneBankAll.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));

                    }
                    //判断是否是近3个月的数据
                    if (dateAccountOpened <= 3 && dateAccountOpened >= 0) {
                        threeBankAll = threeBankAll.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                    }
                    //判断是否是近6个月的数据
                    if (dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                        sixBankAll = sixBankAll.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                    }
                }

            }
        }

        if (accountNo.size() != 0) {
            ruleInNextStep.setXdspaymenthistoryamountbankavgac(avg.divide(new BigDecimal(accountNo.size()), 0, BigDecimal.ROUND_DOWN).toString());
        }

        if (threeMonthAccountNo.size() != 0) {
            ruleInNextStep.setXdspaymenthistoryamountbankavgac3m(threeMonthAvg
                    .divide(new BigDecimal(threeMonthAccountNo.size()), 0, BigDecimal.ROUND_DOWN).toString()
            );
        }

        if (sixMonthAccountNo.size() != 0) {
            ruleInNextStep.setXdspaymenthistoryamountbankavgac6m(sixMonthAvg
                    .divide(new BigDecimal(sixMonthAccountNo.size()), 0, BigDecimal.ROUND_DOWN).toString()
            );
        }

        ruleInNextStep.setXpcntbankover60d12m(over60Num + "");
        ruleInNextStep.setXdspaymenthistoryamountbanklast(pre == null ? "-1" : pre.getString("OpeningBalanceAmt"));

        ruleInNextStep.setXdspaymenthistorycntbankavg1m(oneBankAccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorycntbankavg3m(new BigDecimal(threeMonthAccountNo.size()).divide(new BigDecimal("3"), 0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistorycntbankavg6m(new BigDecimal(sixMonthAccountNo.size()).divide(new BigDecimal("6"), 0, BigDecimal.ROUND_DOWN).toString());

        ruleInNextStep.setXdspaymenthistorynumbank(bankNum.size() + "");
        ruleInNextStep.setXdspaymenthistoryamountbankall(bankAll.toString());
        ruleInNextStep.setXdspaymenthistoryamountbank1m(oneBankAll.toString());
        ruleInNextStep.setXdspaymenthistoryamountbank3m(threeBankAll.toString());
        ruleInNextStep.setXdspaymenthistoryamountbank6m(sixBankAll.toString());
        ruleInNextStep.setXdspaymenthistoryamountbankmax(preBalanceAmt + "");
        ruleInNextStep.setXdspaymenthistoryamountbankmin(preMinAmt == null ? "-1" : preMinAmt + "");
        ruleInNextStep.setXdspaymenthistoryamountbankavg3m(threeBankAll.divide(new BigDecimal("3"), 0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountbankavg6m(sixBankAll.divide(new BigDecimal("6"), 0, BigDecimal.ROUND_DOWN).toString());


    }


    private void analysePerformanceWithPerforming(JSONArray accountMonthlyPaymentHistory, RuleInNextStepXds ruleInNextStep,CustomerXdsReport ctcustomerXdsReport) {

        List<String> new3AccountNo = new ArrayList<>();
        List<String> close6AccountNo = new ArrayList<>();
        List<String> close3AccountNo = new ArrayList<>();
        List<String> new6AccountNo = new ArrayList<>();

        BigDecimal new3 = BigDecimal.ZERO;

        BigDecimal new6 = BigDecimal.ZERO;

        BigDecimal close3 = BigDecimal.ZERO;
        BigDecimal close6 = BigDecimal.ZERO;

        for (int i = 0; i < accountMonthlyPaymentHistory.size(); i++) {
            JSONObject jsonObject = accountMonthlyPaymentHistory.getJSONObject(i);

            int dateAccountOpened = DateUtils.getMonthInterval(DateUtils.getTimeFromDate(ctcustomerXdsReport.getCreateTime()),
                    DateUtils.timeFormat(jsonObject.getString("DateAccountOpened")));

            if ("Performing".equals(jsonObject.getString("PerformanceStatus"))) {
//                近三个月新增在贷合同数
                if (!new3AccountNo.contains(jsonObject.getString("AccountNo")) && dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    new3AccountNo.add(jsonObject.getString("AccountNo"));
                }
//近三个月关闭在贷合同数
                if (!close3AccountNo.contains(jsonObject.getString("AccountNo")) && "Closed".equals(jsonObject.getString("AccountStatus")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    close3AccountNo.add(jsonObject.getString("AccountNo"));
                }

                if (dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    new3 = new3.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }
//近三个月关闭在贷合同金额
                if ("Closed".equals(jsonObject.getString("AccountStatus")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    close3 = close3.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));

                }

//                近六个月新增在贷合同数
                if (!new6AccountNo.contains(jsonObject.getString("AccountNo")) &&
                        dateAccountOpened <= 6 && dateAccountOpened >= 0) {
                    new6AccountNo.add(jsonObject.getString("AccountNo"));
                }

//                近六个月关闭在贷合同数
                if (!close6AccountNo.contains(jsonObject.getString("AccountNo")) && "Closed".equals(jsonObject.getString("AccountStatus")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    close6AccountNo.add(jsonObject.getString("AccountNo"));
                }

//                近六个月新增在贷合同金额
                if (dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    new6 = new6.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }

                //                近六个月关闭在贷合同金额
                if ("Closed".equals(jsonObject.getString("AccountStatus")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    close6 = close6.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }
            }
        }

        ruleInNextStep.setXdspaymenthistorynumperformingnew3m(new3AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumperformingnew6m(new6AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumperformingclosed3m(close3AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumperformingclosed6m(close6AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistoryamountperformingnew3m(new3.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountperformingnew6m(new6.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountperformingclosed3m(close3.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountperformingclosed6m(close6.setScale(0, BigDecimal.ROUND_DOWN).toString());
    }

    private void analysePerformanceWithDoubtful(JSONArray accountMonthlyPaymentHistory, RuleInNextStepXds ruleInNextStep,CustomerXdsReport ctcustomerXdsReport) {

        List<String> doubtful6AccountNo = new ArrayList<>();
        List<String> doubtful12AccountNo = new ArrayList<>();
        List<String> doubtful24AccountNo = new ArrayList<>();
        List<String> doubtful24MoreAccountNo = new ArrayList<>();

        List<String> new3AccountNo = new ArrayList<>();
        List<String> new6AccountNo = new ArrayList<>();
        List<String> close3AccountNo = new ArrayList<>();
        List<String> close6AccountNo = new ArrayList<>();

        BigDecimal new3 = BigDecimal.ZERO;
        BigDecimal new6 = BigDecimal.ZERO;
        BigDecimal close3 = BigDecimal.ZERO;
        BigDecimal close6 = BigDecimal.ZERO;

        int doubtful6Num = 0;
        int doubtful12Num = 0;
        int doubtful24Num = 0;
        int doubtful24MoreNum = 0;

        for (int i = 0; i < accountMonthlyPaymentHistory.size(); i++) {
            JSONObject jsonObject = accountMonthlyPaymentHistory.getJSONObject(i);

            if ("Doubtful".equals(jsonObject.getString("PerformanceStatus"))) {
                List<LocalDateTime> suitTimeInterval = getSuitTimeInterval(jsonObject, ctcustomerXdsReport.getCreateTime());
//                6个月内呆账合同数
                if (!doubtful6AccountNo.contains(jsonObject.getString("AccountNo"))) {
                    doubtful6AccountNo.add(jsonObject.getString("AccountNo"));
//                    判断时间是否在6个月内
                    if (suitTimeInterval != null && DateUtils.getNearMonth(suitTimeInterval.get(1), suitTimeInterval.get(0), 6, 0)) {
                        doubtful6Num++;
                    }
                }

                //                7至12个月内呆账合同数
                if (!doubtful12AccountNo.contains(jsonObject.getString("AccountNo"))) {
                    doubtful12AccountNo.add(jsonObject.getString("AccountNo"));
//                    判断时间是否在 7 - 12 个月
                    if (suitTimeInterval != null && DateUtils.getNearMonth(suitTimeInterval.get(1), suitTimeInterval.get(0), 12, 7)) {
                        doubtful12Num++;
                    }
                }

                //                13至24个月内呆账合同数
                if (!doubtful24AccountNo.contains(jsonObject.getString("AccountNo"))) {
                    doubtful24AccountNo.add(jsonObject.getString("AccountNo"));
//                    判断时间是否在 13 - 24 个月
                    if (suitTimeInterval != null && DateUtils.getNearMonth(suitTimeInterval.get(1), suitTimeInterval.get(0), 24, 13)) {
                        doubtful24Num++;
                    }
                }

                //                24个月以上呆账合同数
                if (!doubtful24MoreAccountNo.contains(jsonObject.getString("AccountNo"))) {
                    doubtful24MoreAccountNo.add(jsonObject.getString("AccountNo"));
//                    判断时间是否大于 24
                    if (suitTimeInterval != null && DateUtils.getNearMonth(suitTimeInterval.get(1), suitTimeInterval.get(0), null, 25)) {
                        doubtful24MoreNum++;
                    }
                }

                int dateAccountOpened = DateUtils.getMonthInterval(DateUtils.getTimeFromDate(ctcustomerXdsReport.getCreateTime()),
                        DateUtils.timeFormat(jsonObject.getString("DateAccountOpened")));
                //                近三个月新增呆账合同数
                if (!new3AccountNo.contains(jsonObject.getString("AccountNo")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    new3AccountNo.add(jsonObject.getString("AccountNo"));
                }
                //                近六个月新增呆账合同数
                if (!new6AccountNo.contains(jsonObject.getString("AccountNo")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    new6AccountNo.add(jsonObject.getString("AccountNo"));
                }

                //                近六个月关闭呆账合同数
                if ("Closed".equals(jsonObject.getString("AccountStatus")) && !close6AccountNo.contains(jsonObject.getString("AccountNo")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    close6AccountNo.add(jsonObject.getString("AccountNo"));
                }

                //                近三个月新增呆账合同金额
                if (dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    new3 = new3.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }

                //                近六个月新增呆账合同金额
                if (dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    new6 = new6.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }

                //                近三个月关闭呆账合同金额
                if ("Closed".equals(jsonObject.getString("AccountStatus")) && dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    close3 = close3.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }

                //                近六个月关闭呆账合同金额
                if ("Closed".equals(jsonObject.getString("AccountStatus")) && dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    close6 = close6.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }

                //                近三个月关闭呆账合同数
                if ("Closed".equals(jsonObject.getString("AccountStatus")) &&
                        !close3AccountNo.contains(jsonObject.getString("AccountNo")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    close3AccountNo.add(jsonObject.getString("AccountNo"));
                }
            }
        }

        ruleInNextStep.setXdspaymenthistorynumdoubtful6m(doubtful6Num + "");
        ruleInNextStep.setXdspaymenthistorynumdoubtful12m(doubtful12Num + "");
        ruleInNextStep.setXdspaymenthistorynumdoubtful24m(doubtful24Num + "");
        ruleInNextStep.setXdspaymenthistorynumdoubtfulover24m(doubtful24MoreNum + "");
        ruleInNextStep.setXdspaymenthistorynumdoubtfulnew3m(new3AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumdoubtfulnew6m(new6AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumdoubtfulclosed3m(close3AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumdoubtfulclosed6m(close6AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistoryamountdoubtfulnew3m(new3.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountdoubtfulnew6m(new6.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountdoubtfulclosed3m(close3.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountdoubtfulclosed6m(close6.setScale(0, BigDecimal.ROUND_DOWN).toString());
    }

    private void analysePerformanceWithLost(JSONArray accountMonthlyPaymentHistory, RuleInNextStepXds ruleInNextStep,CustomerXdsReport ctcustomerXdsReport) {

        List<String> lost6AccountNo = new ArrayList<>();
        List<String> lost12AccountNo = new ArrayList<>();
        List<String> lost24AccountNo = new ArrayList<>();
        List<String> lost24MoreAccountNo = new ArrayList<>();
        List<String> new3AccountNo = new ArrayList<>();
        List<String> new6AccountNo = new ArrayList<>();
        List<String> close3AccountNo = new ArrayList<>();
        List<String> close6AccountNo = new ArrayList<>();

        BigDecimal lost3 = BigDecimal.ZERO;
        BigDecimal new6 = BigDecimal.ZERO;
        BigDecimal close3 = BigDecimal.ZERO;
        BigDecimal close6 = BigDecimal.ZERO;

        int lost6Num = 0;
        int lost12Num = 0;
        int lost24Num = 0;
        int lost24MoreNum = 0;

        for (int i = 0; i < accountMonthlyPaymentHistory.size(); i++) {
            JSONObject jsonObject = accountMonthlyPaymentHistory.getJSONObject(i);
            if ("Lost".equals(jsonObject.getString("PerformanceStatus"))) {
                List<LocalDateTime> suitTimeInterval = getSuitTimeInterval(jsonObject, ctcustomerXdsReport.getCreateTime());
                //                6个月内坏账合同数
                if (!lost6AccountNo.contains(jsonObject.getString("AccountNo"))) {
                    lost6AccountNo.add(jsonObject.getString("AccountNo"));
//                    判断时间是否在6个月内
                    if (suitTimeInterval != null && DateUtils.getNearMonth(suitTimeInterval.get(1), suitTimeInterval.get(0), 6, 0)) {
                        lost6Num++;
                    }
                }

                //                7-12个月内坏账合同数
                if (!lost12AccountNo.contains(jsonObject.getString("AccountNo"))) {
                    lost12AccountNo.add(jsonObject.getString("AccountNo"));
//                    判断时间是否在7-12
                    if (suitTimeInterval != null && DateUtils.getNearMonth(suitTimeInterval.get(1), suitTimeInterval.get(0), 12, 7)) {
                        lost12Num++;
                    }
                }

                //                13-24个月内坏账合同数
                if (!lost24AccountNo.contains(jsonObject.getString("AccountNo"))) {
                    lost24AccountNo.add(jsonObject.getString("AccountNo"));
//                    判断时间是否在13-24
                    if (suitTimeInterval != null && DateUtils.getNearMonth(suitTimeInterval.get(1), suitTimeInterval.get(0), 24, 13)) {
                        lost24Num++;
                    }
                }

                //                24个月以上坏账合同数
                if (!lost24MoreAccountNo.contains(jsonObject.getString("AccountNo"))) {
                    lost24MoreAccountNo.add(jsonObject.getString("AccountNo"));
//                    判断时间是否在24以上
                    if (suitTimeInterval != null && DateUtils.getNearMonth(suitTimeInterval.get(1), suitTimeInterval.get(0), null, 25)) {
                        lost24MoreNum++;
                    }
                }
                int dateAccountOpened = DateUtils.getMonthInterval(DateUtils.getTimeFromDate(ctcustomerXdsReport.getCreateTime()),
                        DateUtils.timeFormat(jsonObject.getString("DateAccountOpened")));
                //                近三个月新增坏账合同数
                if (!new3AccountNo.contains(jsonObject.getString("AccountNo")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    new3AccountNo.add(jsonObject.getString("AccountNo"));
                }

                //                近六个月新增坏账合同数
                if (!new6AccountNo.contains(jsonObject.getString("AccountNo")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    new6AccountNo.add(jsonObject.getString("AccountNo"));
                }

                //                近三个月关闭坏账合同数
                if (!close3AccountNo.contains(jsonObject.getString("AccountNo")) && "Closed".equals(jsonObject.getString("AccountStatus")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    close3AccountNo.add(jsonObject.getString("AccountNo"));
                }

                //                近六个月关闭坏账合同数
                if (!close6AccountNo.contains(jsonObject.getString("AccountNo")) && "Closed".equals(jsonObject.getString("AccountStatus")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    close6AccountNo.add(jsonObject.getString("AccountNo"));
                }

                //                近三个月新增坏账合同金额
                if (dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    lost3 = lost3.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }

                //                近六个月新增坏账合同金额
                if (dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    new6 = new6.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }

                //                近六个月关闭坏账合同金额
                if ("Closed".equals(jsonObject.getString("AccountStatus")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    close6 = close6.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }

                //                近三个月关闭坏账合同金额
                if ("Closed".equals(jsonObject.getString("AccountStatus")) && dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    close3 = close3.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }
            }
        }

        ruleInNextStep.setXdspaymenthistorynumlost6m(lost6Num + "");
        ruleInNextStep.setXdspaymenthistorynumlost12m(lost12Num + "");
        ruleInNextStep.setXdspaymenthistorynumlost24m(lost24Num + "");
        ruleInNextStep.setXdspaymenthistorynumlostover24m(lost24MoreNum + "");
        ruleInNextStep.setXdspaymenthistorynumlostclosed3m(close3AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumlostclosed6m(close6AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumlostnew3m(new3AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumlostnew6m(new6AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistoryamountlostnew3m(lost3.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountlostclosed3m(close3.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountlostnew6m(new6.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountlostclosed6m(close6.setScale(0, BigDecimal.ROUND_DOWN).toString());

    }

    private void analysePerformanceWithSubstandard(JSONArray accountMonthlyPaymentHistory, RuleInNextStepXds ruleInNextStep,CustomerXdsReport ctcustomerXdsReport) {

        List<String> sub6AccountNo = new ArrayList<>();
        List<String> sub12AccountNo = new ArrayList<>();
        List<String> sub24AccountNo = new ArrayList<>();
        List<String> new3AccountNo = new ArrayList<>();
        List<String> new6AccountNo = new ArrayList<>();
        List<String> close3AccountNo = new ArrayList<>();
        List<String> close6AccountNo = new ArrayList<>();
        // 匹配数量
        int sub6Num = 0;
        int sub12Num = 0;
        int sub24Num = 0;
        int sub24MoreNum = 0;
        List<String> sub24MoreAccountNo = new ArrayList<>();

        BigDecimal new3 = BigDecimal.ZERO;
        BigDecimal new6 = BigDecimal.ZERO;
        BigDecimal close3 = BigDecimal.ZERO;
        BigDecimal close6 = BigDecimal.ZERO;

        for (int i = 0; i < accountMonthlyPaymentHistory.size(); i++) {
            JSONObject jsonObject = accountMonthlyPaymentHistory.getJSONObject(i);
            List<LocalDateTime> suitTimeInterval = getSuitTimeInterval(jsonObject, ctcustomerXdsReport.getCreateTime());
            if ("Substandard".equals(jsonObject.getString("PerformanceStatus"))) {
                //                6个月内次贷合同数
                if (!sub6AccountNo.contains(jsonObject.getString("AccountNo"))) {
                    sub6AccountNo.add(jsonObject.getString("AccountNo"));
//                    判断时间是否在6个月内
                    if (suitTimeInterval != null && DateUtils.getNearMonth(suitTimeInterval.get(1), suitTimeInterval.get(0), 6, 0)) {
                        sub6Num++;
                    }
                }

                //                7-12个月内次贷合同数
                if (!sub12AccountNo.contains(jsonObject.getString("AccountNo"))) {
                    sub12AccountNo.add(jsonObject.getString("AccountNo"));
//                    判断时间是否在7-12
                    if (suitTimeInterval != null && DateUtils.getNearMonth(suitTimeInterval.get(1), suitTimeInterval.get(0), 12, 7)) {
                        sub12Num++;
                    }
                }

                //                13-24个月内次贷合同数
                if (!sub24AccountNo.contains(jsonObject.getString("AccountNo"))) {
                    sub24AccountNo.add(jsonObject.getString("AccountNo"));
//                    判断时间是否在13-24
                    if (suitTimeInterval != null && DateUtils.getNearMonth(suitTimeInterval.get(1), suitTimeInterval.get(0), 24, 13)) {
                        sub24Num++;
                    }
                }

                //                24个月以上次贷合同数
                if (!sub24MoreAccountNo.contains(jsonObject.getString("AccountNo"))) {
                    sub24MoreAccountNo.add(jsonObject.getString("AccountNo"));
//                    判断时间是否在24以上
                    if (suitTimeInterval != null && DateUtils.getNearMonth(suitTimeInterval.get(1), suitTimeInterval.get(0), null, 25)) {
                        sub24MoreNum++;
                    }
                }
                int dateAccountOpened = DateUtils.getMonthInterval(DateUtils.getTimeFromDate(ctcustomerXdsReport.getCreateTime()),
                        DateUtils.timeFormat(jsonObject.getString("DateAccountOpened")));

                //                近三个月新增次贷合同数
                if (!new3AccountNo.contains(jsonObject.getString("AccountNo")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    new3AccountNo.add(jsonObject.getString("AccountNo"));
                }

                //                近三个月关闭次贷合同数
                if ("Closed".equals(jsonObject.getString("AccountStatus")) && !close3AccountNo.contains(jsonObject.getString("AccountNo")) &&
                        dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    close3AccountNo.add(jsonObject.getString("AccountNo"));
                }

                //                近三个月新增次贷合同金额
                if (dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    new3 = new3.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }

                //                近六个月新增次贷合同金额
                if (dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    new6 = new6.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }

                //                近三个月关闭次贷合同金额
                if (dateAccountOpened >= 0 && dateAccountOpened <= 3) {
                    close3 = close3.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }

                //                近六个月关闭次贷合同金额
                if ("Closed".equals(jsonObject.getString("AccountStatus")) && dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    close6 = close6.add(getBigDecimal(jsonObject.getString("OpeningBalanceAmt")));
                }

                //                近六个月新增次贷合同数
                if (!new6AccountNo.contains(jsonObject.getString("AccountNo")) && dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    new6AccountNo.add(jsonObject.getString("AccountNo"));
                }

                //                近六个月关闭次贷合同数
                if ("Closed".equals(jsonObject.getString("AccountStatus")) &&
                        !close6AccountNo.contains(jsonObject.getString("AccountNo")) && dateAccountOpened >= 0 && dateAccountOpened <= 6) {
                    close6AccountNo.add(jsonObject.getString("AccountNo"));
                }
            }
        }

        ruleInNextStep.setXdspaymenthistorynumsubstandard6m(sub6Num + "");
        ruleInNextStep.setXdspaymenthistorynumsubstandard12m(sub12Num + "");
        ruleInNextStep.setXdspaymenthistorynumsubstandard24m(sub24Num + "");
        ruleInNextStep.setXdspaymenthistorynumsubstandardover24m(sub24MoreNum + "");
        ruleInNextStep.setXdspaymenthistorynumsubstandardnew3m(new3AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumsubstandardclosed3m(close3AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumsubstandardclosed3m(close6AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistorynumsubstandardnew6m(new6AccountNo.size() + "");
        ruleInNextStep.setXdspaymenthistoryamountsubstandardnew3m(new3.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountsubstandardnew6m(new6.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountsubstandardclosed3m(close3.setScale(0, BigDecimal.ROUND_DOWN).toString());
        ruleInNextStep.setXdspaymenthistoryamountsubstandardclosed6m(close6.setScale(0, BigDecimal.ROUND_DOWN).toString());
    }

    public static Integer getMData(JSONObject jsonObject1, String name) {
        if (StringUtils.isNotBlank(jsonObject1.getString(name)) && !"#".equals(jsonObject1.getString(name)) && !"*".equals(jsonObject1.getString(name))) {
            return jsonObject1.getInteger(name);
        } else {
            return -1;
        }
    }


    /**
     * 获取对象中 M1 ~ M[maxRange] 中 >= 60 中符合数据的数量
     *
     * @param jsonObject 对象
     * @param maxRange   M最大的取值范围
     * @param moreValue  超过多少加入比较
     * @return 最大值
     */
    private int getSuitNum(JSONObject jsonObject, int maxRange, int moreValue) {
        if (jsonObject == null) {
            return 0;
        }
        int result = 0;
        for (int i = 1; i <= maxRange; i++) {
            String value = jsonObject.getString("M" + String.format("%02d", i));
            if (value == null) {
                continue;
            }
            if ("#".equals(value)) {
                continue;
            }
            int compareValue = 0;
            try {
                compareValue = Integer.parseInt(value);
            } catch (NumberFormatException ignore) {
            }
            if (compareValue >= moreValue) {
                result++;
            }
        }
        return result;
    }

    /**
     * 获取时间间隔
     */
    private List<LocalDateTime> getSuitTimeInterval(JSONObject jsonObject, String timeString) {
        String closedDate = jsonObject.getString("ClosedDate");
        LocalDateTime compareTime;
        if (StringUtils.isBlank(closedDate) || DateUtils.checkTimeUpNow(closedDate)) {
            compareTime = DateUtils.timeFormat(jsonObject.getString("LastUpdatedDate"));
        } else {
            compareTime = DateUtils.timeFormat(closedDate);
        }
        if (compareTime == null) {
            return null;
        }
        LocalDateTime time = DateUtils.getTimeFromString(timeString);
        if (time == null) {
            return null;
        }
        return Arrays.asList(time, compareTime);
    }

    /**
     * 获取时间间隔
     */
    private List<LocalDateTime> getSuitTimeInterval(JSONObject jsonObject, Date date) {
        String closedDate = jsonObject.getString("ClosedDate");
        LocalDateTime compareTime;
        if (StringUtils.isBlank(closedDate) || DateUtils.checkTimeUpNow(closedDate)) {
            compareTime = DateUtils.timeFormat(jsonObject.getString("LastUpdatedDate"));
        } else {
            compareTime = DateUtils.timeFormat(closedDate);
        }
        if (compareTime == null) {
            return null;
        }
        LocalDateTime time = DateUtils.getTimeFromDate(date);
        if (time == null) {
            return null;
        }
        return Arrays.asList(time, compareTime);
    }

    private static RuleInNextStepXds afterFormatRuleInNextStepXds(RuleInNextStepXds ruleInNextStep) {

        ruleInNextStep = RuleUtils.afterFormat(ruleInNextStep, "xdsId", "nextStepId");

        if ("-1".equals(ruleInNextStep.getState())) {
            ruleInNextStep.setState(UserConstants.BUSINESS_NORMAL);
        }

        return ruleInNextStep;
    }


    /**
     * 获取两个对象中时间较大的对象
     *
     * @param pre     pre
     * @param compare compare
     * @return 对象
     */
    private JSONObject maxDate(JSONObject pre, JSONObject compare) {

        LocalDateTime time = DateUtils.timeFormat(pre.getString("DateAccountOpened"));
        LocalDateTime compareTime = DateUtils.timeFormat(compare.getString("DateAccountOpened"));
        //        过滤空数据
        if (time == null && compareTime == null) {
            return pre;
        }
        if (time == null) {
            return compare;
        }
        if (compareTime == null) {
            return pre;
        }

        if (time.toEpochSecond(ZoneOffset.of("+8")) > compareTime.toEpochSecond(ZoneOffset.of("+8"))) {
            return pre;
        } else {
            return compare;
        }
    }

    public BigDecimal getBigDecimal(String value) {
        if (StringUtils.isBlank(value)) {
            return BigDecimal.ZERO;
        }
        value = value.replaceAll(",", "");
        BigDecimal result = null;
        try {
            result = new BigDecimal(value);
        } catch (Exception ignore) {
        }
        return result;
    }
}
